// miniprogram/pages/youka/youka.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  words:[],
    imgurl:'http://azuokyf.gitee.io/philatelic_pictures/img/',
    //存放图片地址
    img: ['mycard_icon_changkuai.png', 'mycard_icon_changkuai.png', 'mycard_icon_changkuai.png','mycard_icon_changkuai.png'],
    flag:[0,0,0,0],
    //保存下标
    index:[],
    selt:0,
    // 保存已经选中的下标
    arr:[],
    jieguo:true,
    jiang1:true,
    jiang2: true,
    jiang3: true,
    jiang4: true,
    prize:null
  },
   choujiang(e){
    //  console.log('e',e);
     let number = e.currentTarget.dataset.number
     let index = e.currentTarget.dataset.index;
    //  let check = e.currentTarget.database.check;
    //  console.log(check)
    //该卡片的个数为0时返回
    if(number<=0){
      // console.log('不显示')
      return;
    }else{
      // console.log('显示')
      //为0时表示未选中
      if (this.data.flag[index] === 0 && this.data.selt<2){
        // console.log(this.data.flag[index])

        //改为1，表示选中
        this.data.flag[index]=1
        console.log(this.data.flag[index])
        let up = 'img['+index+']'
        //改变图片
        this.setData({
          [up]:'mycard_icon_selected.png'
        })
        this.data.selt+=1;
      } else if (this.data.flag[index] === 1 && this.data.selt <= 2){
        this.data.flag[index] = 0
        console.log(this.data.flag[index])
        let up = 'img[' + index + ']'
        this.setData({
          [up]: 'mycard_icon_changkuai.png'
        })
        this.data.selt -= 1;
      }
      else{
        this.data.flag[index] = 0
        console.log(this.data.flag[index])
        let up = 'img[' + index + ']'
        this.setData({
          [up]:'mycard_icon_changkuai.png'
        })
         
      }
    }
   },
   
  //  if(this.data.cishu === 0){

  //  }
   hecheng(){
        if(this.data.prize == null){
          var num = 0, arr = [], push, nums = 0;
          for (let i = 0; i < this.data.flag.length; i++) {
            num += this.data.flag[i]
            if (this.data.flag[i] === 1) {
              push = 'arr[' + nums + ']'
              this.setData({
                [push]: i
              })
              nums++;
            }
          }
          console.log(this.data.arr)
          //  console.log(arr)
          if (num !== 2) {
            wx.showToast({
              title: '请选择两张图片',
            })
            return;
          } else {
            let self = this
            //  console.log(this.data.arr[0])
            wx.showLoading({
              title: '正在合成...',
            })
            console.log('合成')
            wx.cloud.callFunction({
              name: 'hecheng',
              data: {
                index1: self.data.arr[0],
                index2: self.data.arr[1]
              },
              success(res) {
                console.log(res)
                let word = res.result.word
                let index1 = res.result.index1
                let index2 = res.result.index2
                let jiang = 'jiang' + res.result.jiang
                console.log(word)
                self.setData({
                  words: word
                })
                self.data.flag[index1] = 0
                self.data.flag[index2] = 0
                console.log(self.data.flag[index1], self.data.flag[index2])
                let up1 = 'img[' + index1 + ']'
                let up2 = 'img[' + index2 + ']'
                self.data.selt = 0;
                self.setData({
                  [up1]: 'mycard_icon_changkuai.png'
                })
                self.setData({
                  [jiang]: false
                })
                self.setData({
                  [up2]: 'mycard_icon_changkuai.png'
                })
                self.setData({
                  jieguo: false
                })
                wx.showToast({
                  title: '合成完成',
                  duration: 500
                })
              },
              fail: e => {
                //  console.error('[上传文件] 失败：', e)
                wx.showToast({
                  icon: 'none',
                  title: '合成失败，请稍后再试',
                })
              },
            })
          }
        }else{
          wx.showToast({
            title: '已经合成过',
          })
        }
       
  
   },
  toduijiang(){
   wx.redirectTo({
     url: '../duijiang/duijiang',
   })
  },
  fanhui(){
    wx.redirectTo({
      url: '../index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 const db = wx.cloud.database();
 const jiyou = db.collection('jiyou');
 jiyou.where({
   '_openid':app.globalData.openid
 }).get().then((res)=>{
   console.log('查询成功',res.data[0].words)
   if(res.data.length>0){
     let words = res.data[0].words
     let prize = res.data[0].prize
     this.setData({
       words,
       prize
     })
   }
   
 })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})